﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;

namespace TradeMarkSys
{
    public partial class FrmApproval : Form
    {
        public object TradeMarkID { set; get; }
        public FrmApproval()
        {
            InitializeComponent();
        }

        private void FrmApproval_Load(object sender, EventArgs e)
        {
            OleDbConnection conn = null;
            try
            {
                string sql = string.Format(@"select * from TradeMark where id = {0}", TradeMarkID);
                string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                conn = new OleDbConnection(connstr);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                OleDbDataAdapter da = new OleDbDataAdapter();
                da.SelectCommand = cmd;
                DataSet ds = new DataSet();
                da.Fill(ds);
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    txtBaseInfo.Text = dr["BaseInfo"].ToString();
                    txtRegCode.Text = dr["RegCode"].ToString();
                    txtTel.Text = dr["Tel"].ToString();
                    txtCommissionFee.Text = dr["CommissionFee"].ToString();
                    txtApplicant.Text = dr["Applicant"].ToString();
                    txtKindCode.Text = dr["KindCode"].ToString();
                    txtAddress.Text = dr["Address"].ToString();
                    txtTradeMarkName.Text = dr["TradeMarkName"].ToString();
                    cmbApplyProject.Text = dr["ApplyProject"].ToString();
                    dateTimePickerApplyDate.Value = DateTime.Parse(dr["ApplyDate"].ToString());
                    txtApplyProjectRemark.Text = dr["ApplyProjectRemark"].ToString();
                    checkBoxAccept.Checked = dr["Accept"] == null ? false : bool.Parse(dr["Accept"].ToString());
                    dateTimePickerAcceptDate.Value = DateTime.Parse(dr["AcceptDate"].ToString());
                    txtAcceptRemark.Text = dr["AcceptRemark"].ToString();
                    checkBoxCorrections.Checked = dr["Corrections"] == null ? false : bool.Parse(dr["Corrections"].ToString());
                    dateTimePickerCorrectionsDate.Value = DateTime.Parse(dr["CorrectionsDate"].ToString());
                    txtCorrectionsRemark.Text = dr["CorrectionsRemark"].ToString();
                    checkBoxApprove.Checked = dr["Approve"] == null ? false : bool.Parse(dr["Approve"].ToString());
                    dateTimePickerApproveDate.Value = DateTime.Parse(dr["ApproveDate"].ToString());
                    txtApproveRemark.Text = dr["ApproveRemark"].ToString();
                    checkBoxFirstTrial.Checked = dr["FirstTrial"] == null ? false : bool.Parse(dr["FirstTrial"].ToString());
                    dateTimePickerFirstTrialDate.Value = DateTime.Parse(dr["FirstTrialDate"].ToString());
                    txtFirstTrialRemark.Text = dr["FirstTrialRemark"].ToString();
                    checkBoxDissent.Checked = dr["Dissent"] == null ? false : bool.Parse(dr["Dissent"].ToString());
                    dateTimePickerDissentDate.Value = DateTime.Parse(dr["DissentDate"].ToString());
                    txtDissentRemark.Text = dr["DissentRemark"].ToString();
                    checkBoxNotice.Checked = dr["Notice"] == null ? false : bool.Parse(dr["Notice"].ToString());
                    dateTimePickerNoticeDate.Value = DateTime.Parse(dr["NoticeDate"].ToString());
                    dateTimePickerValidityDate.Value = DateTime.Parse(dr["ValidityDate"].ToString());
                    txtNoticeRemark.Text = dr["NoticeRemark"].ToString();
                    checkBoxCertificate.Checked = dr["Certificate"] == null ? false : bool.Parse(dr["Certificate"].ToString());
                    dateTimePickerCertificateDate.Value = DateTime.Parse(dr["CertificateDate"].ToString());
                    txtCertificateRemark.Text = dr["CertificateRemark"].ToString();
                    checkBoxCharge.Checked = dr["Charge"] == null ? false : bool.Parse(dr["Charge"].ToString());
                    txtChargeFee.Text = dr["ChargeFee"].ToString();
                    dateTimePickerChargeDate.Value = DateTime.Parse(dr["ChargeDate"].ToString());
                    txtChargeRemark.Text = dr["ChargeRemark"].ToString();
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }

        private void checkBoxAccept_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxAccept.Checked)
                {
                    dateTimePickerAcceptDate.Enabled = true;
                    txtAcceptRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerAcceptDate.Enabled = false;
                    txtAcceptRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxCorrections_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxCorrections.Checked)
                {
                    dateTimePickerCorrectionsDate.Enabled = true;
                    txtCorrectionsRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerCorrectionsDate.Enabled = false;
                    txtCorrectionsRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxApprove_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxApprove.Checked)
                {
                    dateTimePickerApproveDate.Enabled = true;
                    txtApproveRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerApproveDate.Enabled = false;
                    txtApproveRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxFirstTrial_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxFirstTrial.Checked)
                {
                    dateTimePickerFirstTrialDate.Enabled = true;
                    txtFirstTrialRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerFirstTrialDate.Enabled = false;
                    txtFirstTrialRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxDissent_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxDissent.Checked)
                {
                    dateTimePickerDissentDate.Enabled = true;
                    txtDissentRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerDissentDate.Enabled = false;
                    txtDissentRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxNotice_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxNotice.Checked)
                {
                    dateTimePickerNoticeDate.Enabled = true;
                    dateTimePickerValidityDate.Enabled = true;
                    txtNoticeRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerAcceptDate.Enabled = false;
                    dateTimePickerValidityDate.Enabled = false;
                    txtNoticeRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxCertificate_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxCertificate.Checked)
                {
                    dateTimePickerCertificateDate.Enabled = true;
                    txtCertificateRemark.Enabled = true;
                }
                else
                {
                    dateTimePickerCertificateDate.Enabled = false;
                    txtCertificateRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void checkBoxCharge_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (checkBoxCharge.Checked)
                {
                    txtChargeFee.Enabled = true;
                    dateTimePickerChargeDate.Enabled = true;
                    txtChargeRemark.Enabled = true;
                }
                else
                {
                    txtChargeFee.Enabled = false;
                    dateTimePickerChargeDate.Enabled = false;
                    txtChargeRemark.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = null;
            try
            {
                string sql = string.Format(@"update TradeMark set Accept = {1},AcceptDate = '{2}',AcceptRemark = '{3}',Corrections = {4},CorrectionsDate = '{5}',CorrectionsRemark = '{6}',
Approve = {7},ApproveDate = '{8}',ApproveRemark = '{9}',FirstTrial = {10},FirstTrialDate = '{11}',FirstTrialRemark = '{12}',Dissent = {13},
DissentDate = '{14}',DissentRemark = '{15}',Notice = {16},NoticeDate = '{17}',ValidityDate = '{18}',NoticeRemark = '{19}',Certificate = {20},
CertificateDate = '{21}',CertificateRemark = '{22}',Charge = {23},ChargeFee = {24},ChargeDate = '{25}',ChargeRemark = '{26}' ,
BaseInfo='{27}',RegCode='{28}',Tel='{29}',CommissionFee='{30}',Applicant='{31}',KindCode='{32}',Address='{33}',TradeMarkName='{34}',ApplyProject='{35}',ApplyDate='{36}',ApplyProjectRemark='{37}' 
where id = {0}", 
                                        TradeMarkID, checkBoxAccept.Checked
                                        , dateTimePickerAcceptDate.Value, txtAcceptRemark.Text, checkBoxCorrections.Checked, dateTimePickerCorrectionsDate.Value, 
                                        txtCorrectionsRemark.Text, checkBoxApprove.Checked, dateTimePickerApproveDate.Value, txtApproveRemark.Text, checkBoxFirstTrial.Checked, dateTimePickerFirstTrialDate.Value,
                                        txtFirstTrialRemark.Text, checkBoxDissent.Checked, dateTimePickerDissentDate.Value, txtDissentRemark.Text, checkBoxNotice.Checked,
                                        dateTimePickerNoticeDate.Value, dateTimePickerValidityDate.Value, txtNoticeRemark.Text, checkBoxCertificate.Checked,
                                        dateTimePickerCertificateDate.Value, txtCertificateRemark.Text, checkBoxCharge.Checked, txtChargeFee.Text.Equals("") ? "0" : txtChargeFee.Text,
                                        dateTimePickerChargeDate.Value, txtChargeRemark.Text,

                                        txtBaseInfo.Text, txtRegCode.Text, txtTel.Text,
                                        txtCommissionFee.Text,
                                        txtApplicant.Text, txtKindCode.Text,
                                        txtAddress.Text, txtTradeMarkName.Text, cmbApplyProject.Text, dateTimePickerApplyDate.Value,
                                        txtApplyProjectRemark.Text
                                        );
                sql = sql.Replace("\r\n", " ");
                string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+System.Windows.Forms.Application.StartupPath + "\\TradeMarkSys.mdb";
                conn = new OleDbConnection(connstr);
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("批复成功");
                }
                else
                {
                    MessageBox.Show("批复失败,请重试！");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("对不起系统出现错误");
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }
        }
    }
}
